package com.example.demo.controller;


import com.example.demo.enity.User;
import com.example.demo.service.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PassWordOperationController {

    public static String new_password;

    @Autowired
    UserService userService;

    @Autowired
    RedisTemplate redisTemplate;


    @GetMapping("/password")
    public String password(Model model) {
        String username = LoginExitController.username;
        model.addAttribute("username", username);
        return "password";
    }


    @GetMapping("/checkOldPassword")
    //将该请求的返回值写入到response对象的body区
    //通常用来返回JSON数据（controller的方法返回的对象转换为Jason格式）或者是XML数据（String类型的数据）
    //在使用此注解之后不会再走视图处理器(页面)，而是直接将数据写入到输入流中
    @ResponseBody
    //拿到脚本中利用Jason格式传入的参数：String password
    public String checkOldPassword(String oldPassWord) {
        String message = "";

        Object o = redisTemplate.opsForValue().get("user");
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.convertValue(o, User.class);

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = user.getPassword();
        boolean result = encoder.matches(oldPassWord, password);

        if (result) {

        } else {
            message = "密码输入有误！请重新输入！";
        }
        return message;
    }


    @GetMapping("/getNewPassword")
    @ResponseBody
    public String getNewPassword(String newPassword) {
        new_password = newPassword;
        return new_password;
    }


    @GetMapping("/checkReNewPassword")
    @ResponseBody
    public String checkReNewPassword(String reNewPassword) {
        String message = "";

        if (new_password.equals(reNewPassword)) {

        } else {
            message = "新密码与确认密码不一样！请重新输入！";
        }
        return message;
    }


    @GetMapping("/updatePassWord")
    public String updatePassWord(User user, String newPassword, String reNewPassword, Model model) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        String name = user.getUsername();
        Integer id = userService.getUserByUsername(name).getId();

        if (newPassword.equals(reNewPassword)) {
            try {
                String password = encoder.encode(reNewPassword);
                userService.updateUserPassWordByUser(password, id);

                model.addAttribute("updateError", "");
                return "redirect:/logout";
            } catch (Exception e) {
                model.addAttribute("updateError", "修改失败！");
                model.addAttribute("value", "0");
                return "password";
            }
        } else {
            model.addAttribute("error", "密码修改失败！（新密码与确认密码不一致）");
            model.addAttribute("value", "1");
            String username = LoginExitController.username;
            model.addAttribute("username", username);
            return "password";
        }

    }

}
